package com.ymatou.iapi.smsproxy.testcase;

import java.util.ArrayList;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.smsproxy.parameter.SendCallVerifyCodeBean;
import com.ymatou.iapi.smsproxy.parameter.SendMessageBean;
import com.ymatou.iapi.smsproxy.service.SendCallVerifyCodeCall;
import com.ymatou.iapi.smsproxy.service.SendMessageCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.SendSmsCallService;
import com.ymttest.database.mongo.SmsRecordBean;
import com.ymttest.database.mongo.SmsRecordWapper;
import com.ymttest.utils.TestDataManager;

/**
 * 发送语音验证 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_SendCallVerifyCode {
	private static SendCallVerifyCodeBean sendcallverifycodeBean;
	private static SendCallVerifyCodeCall sendcallverifycodeCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("发送语音验证");
	}

	@Before
	public void caseUp() {
		sendcallverifycodeBean = new SendCallVerifyCodeBean();
		sendcallverifycodeCall = new SendCallVerifyCodeCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(sendcallverifycodeCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	private void verifyDB(SendCallVerifyCodeBean sendm, SendCallVerifyCodeCall sendcallverifycodeCall2) throws Exception {
		//IsCall:true , VerifyCode:5913 , Language:zh-cn , MessageTemplate:null
		String content = "IsCall:true , VerifyCode:" + sendcallverifycodeBean.getCode() + " , Language:";

		if (sendcallverifycodeBean.getLanguage() != null) {
			content += sendcallverifycodeBean.getLanguage();
		}
		content += " , MessageTemplate:null";
		String phone = sendcallverifycodeBean.getPhone();
		Logger.comment("根据appid、content、phone到数据库表SmsRecord中查询相应记录来验证是否存入SmsRecord表");

		List<String> phones = new ArrayList<String>();
		phones.add(sendm.getPhone());

		SmsRecordBean smsRecord = new SmsRecordWapper().getCollectionByAppIdContentPhones(sendm.getAppId(), content, phones);

		Logger.verifyNotNull(smsRecord, "验证数据存入SmsRecord成功");
		Logger.debug(com.ymt.core.tool.FormatUtil.beanToGSONString(smsRecord));
		// {"_id":"201712-654f723a33314e5995bd7826b35df127","AppId":"app-java-teleSign","ApiName":"SendMessage","ChannelId":"java-teleSign","ChannelName":"teleSign","Phones":[SendSmsCallService.TESTPHONE],"Content":"autotest_发送短信_6106515124","CreatedTime":{"$date":"2017-12-05T02:44:30.424Z"},"SendedTime":{"$date":"2017-12-05T02:44:32.360Z"},"ClientIp":"172.16.101.12:61584","ServerIp":"172.16.101.112","MessageAction":6,"Comment":"java
		// app","ResponseMessage":"{\"resource_uri\":
		// \"/v1/verify/2580923D69A8070490409157579C048A\", \"reference_id\":
		// \"2580923D69A8070490409157579C048A\", \"sub_resource\": \"sms\",
		// \"errors\": [], \"verify\": {\"code_state\": \"UNKNOWN\",
		// \"code_entered\": \"\"}, \"status\": {\"updated_on\":
		// \"2017-12-05T02:44:22.871057Z\", \"code\": 290, \"description\":
		// \"Message in progress\"}}","Number":1}
		Logger.verifyEquals(sendm.getAppId(), smsRecord.getAppId(), "验证appid");
		Logger.verifyIsContains(sendm.getPhone(), smsRecord.getPhones().toString(), "验证phone");
		Logger.verifyEquals(content, smsRecord.getContent(), "验证content");
		Logger.verifyEquals("SendCallVerifyCode", smsRecord.getApiName(), "验证ApiName");
		if (sendm.getAppId().equals("app-java-teleSign")) {
			Logger.verifyEquals("testSmsDriver", smsRecord.getChannelId(), "验证ChannelId");
			Logger.verifyEquals("testSmsDriver", smsRecord.getChannelName(), "验证ChannelName");
		} else if (sendm.getAppId().equals("app-java-mw")) {
			Logger.verifyEquals("java-mw", smsRecord.getChannelId(), "验证ChannelId");
			Logger.verifyEquals("mw", smsRecord.getChannelName(), "验证ChannelName");
		} else if (sendm.getAppId().equals("app-java-ctc")) {
			Logger.verifyEquals("java-ctc", smsRecord.getChannelId(), "验证ChannelId");
			Logger.verifyEquals("ctc", smsRecord.getChannelName(), "验证ChannelName");
		} else if (sendm.getAppId().equals("app-java-qq")) {
			Logger.verifyEquals("java-qq", smsRecord.getChannelId(), "验证ChannelId");
			Logger.verifyEquals("qq", smsRecord.getChannelName(), "验证ChannelName");
		} else if (sendm.getAppId().equals("app-java-qq-i")) {
			Logger.verifyEquals("java-qq-i", smsRecord.getChannelId(), "验证ChannelId");
			Logger.verifyEquals("qq", smsRecord.getChannelName(), "验证ChannelName");
		} else if (sendm.getAppId().equals("app-java-dl")) {
			Logger.verifyEquals("java-dl", smsRecord.getChannelId(), "验证ChannelId");
			Logger.verifyEquals("dl", smsRecord.getChannelName(), "验证ChannelName");
		}
		Logger.verifyNotNull(smsRecord.getCreatedTime(), "验证createdtime");
		Logger.verifyNotNull(smsRecord.getSendedTime(), "验证SendedTime");
		Logger.verifyNotNull(smsRecord.getClientIp(), "验证ClientIp");
		Logger.verifyNotNull(smsRecord.getServerIp(), "验证ServerIp");
		Logger.verifyEquals(1, smsRecord.getNumber(), "验证Number");
		// TODO MessageAction 6成功 7失败 8 黑名单
//		Logger.verifyEquals("java app", smsRecord.getComment(), "验证Comment");
		Logger.verifyIsContains(smsRecord.get_id(), sendcallverifycodeCall2.getString("result"), "验证result的id数据一致");
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_SendCallVerifyCode_001() {
		Logger.start(true, "发送语音验证app-java-teleSign成功");
		try {
			sendcallverifycodeBean.setAppId("app-java-teleSign");
			sendcallverifycodeBean.setCode(TestDataManager.getRandomNum(4));
			sendcallverifycodeBean.setLanguage("zh-cn");
			sendcallverifycodeBean.setPhone(SendSmsCallService.TESTPHONE);
			sendcallverifycodeCall.setData(sendcallverifycodeBean);
			sendcallverifycodeCall.callService();
			Logger.verifyEquals("success",sendcallverifycodeCall.getString("errorMessage"),"验证errorMessage");
			Logger.verifyEquals("0",sendcallverifycodeCall.getString("errorCode"),"验证errorCode");
			Logger.verifyEquals("true",sendcallverifycodeCall.getString("success"),"验证success");
			verifyDB(sendcallverifycodeBean,sendcallverifycodeCall);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}